<?php

/**
 * 体验中心
 */

namespace mybackend\controllers;

use Yii;
use common\models\expcenter\ExpCenterModel;
use common\services\expcenter\ExpCenterService;
use common\entities\myhtsq\ActionCode;
use yii\filters\AccessControl;

class ExpCenterController extends BaseController {

    protected $_expCenterService;

    public function __construct($id, $module, ExpCenterService $expCenterService, $config = []) {
        $this->_expCenterService = $expCenterService;
        parent::__construct($id, $module, $config);
    }

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['authorization-expired-remind'],
                        'allow' => true,
                    ],
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ]
        ];
    }

    protected function roleAuthRules() {
        return [
            'actions' => ['GetUserList','AddUser','UpdatePwd','UpdateStatus','GetUserCount'],
            'rules' => [
                [
                    'allow' => true,
                    'actionCodeMaps' => [
                        'GetUserList' => ActionCode::EXP_CENTER_VIEW,
                        'AddUser' => ActionCode::EXP_CENTER_MOD,
                        'UpdatePwd' => ActionCode::EXP_CENTER_MOD,
                        'UpdateStatus' => ActionCode::EXP_CENTER_MOD,
                        'GetUserCount' => ActionCode::EXP_CENTER_VIEW,
                    ]
                ]
            ]
        ];
    }

    public function actionAddUser($username = '', $pwd = '') {
        $this->exitIfNotPost();
        $model = new ExpCenterModel(['scenario' => 'save']);
        if ($model->load(Yii::$app->request->post(), '')) {
            if (!$model->validate()) {
                return $this->json(array(
                            "retCode" => -1,
                            "retMsg" => join("\n", $model->getFirstErrors())
                ));
            }
            $entity = new \common\entities\expcenter\ExpCenterEntity();
            $entity->username = $model->username;
            $entity->pwd = $model->pwd;
            $entity->status = \common\repositories\expcenter\ExpCenterRepository::STATUS_YES;
            $entity->created_on = time();
            $entity->modified_on = time();
            $result = $entity->save();
            if (false === $result) {
                $this->exportJson($result, -1, join('\n', $entity->getFirstErrors()));
            } else {
                $this->exportJson($result, 0, '');
            }

            $this->errorParam();
        }
    }

    public function actionUpdatePwd() {
        $this->exitIfNotPost();
        $uid = Yii::$app->request->post('uid');
        $pwd = Yii::$app->request->post('pwd');
        $status = Yii::$app->request->post('status');
        $result = $this->_expCenterService->updatePwd($uid, $pwd);
        $result = $this->_expCenterService->updateStatus($uid);
        $this->exportJson($result, $this->_expCenterService->getErrCode(), $this->_expCenterService->getErrMsg());
    }

    public function actionUpdateStatus($uid) {
        $this->exitIfNotPost();
        $result = $this->_expCenterService->updateStatus($uid);
        $this->exportJson($result, $this->_expCenterService->getErrCode(), $this->_expCenterService->getErrMsg());
    }

    public function actionGetUserList($limit = 10) {
        $result = [];
//        $list = $this->_expCenterService->getUserList($limit);
        $pageIndex = Yii::$app->request->get('pageIndex', 0);
        $pageSize = Yii::$app->request->get('pageSize', 20);
        $query = \common\entities\expcenter\ExpCenterEntity::find();
        $pages = new \yii\data\Pagination(['totalCount' => $query->count()]);
        $pages->setPageSize($pageSize, true);
        $pages->setPage($pageIndex, true);
        $list = $query->offset($pages->offset)->limit($pages->limit)->all();

        foreach ($list as $item) {
            $tmp = [];
            $tmp['uid'] = $item['uid'];
            $tmp['username'] = $item['username'];
            $tmp['pwd'] = $item['pwd'];
            $tmp['status'] = $item['status'];
            $tmp['created_on'] = $item['created_on'];
            $tmp['created_by'] = $item['created_by'];
            $tmp['modified_on'] = $item['modified_on'];
            $tmp['modified_by'] = $item['modified_by'];
            $tmp['url'] = 'http://www.myscrm.cn/index.php?g=ExpCenter&m=Account&a=expLogin&jump=1&token=yttvfy1398670324&username=' . $item['username'] . '&password=' . $item['pwd'];
            $result[] = $tmp;
        }
        $this->exportJson(['total' => $pages->totalCount, 'list' => $result], $this->_expCenterService->getErrCode(), $this->_expCenterService->getErrMsg());
    }

    public function actionGetUserCount() {
        $result = $this->_expCenterService->getUserCount();
        $this->exportJson($result, $this->_expCenterService->getErrCode(), $this->_expCenterService->getErrMsg());
    }

}
